﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NUnit.Framework;

namespace Universe.IoC.Tests
{
    [TestFixture]
    public class ContainerTests
    {
        [Test]
        public void Test_Circular_Reference()
        {
            try
            {
                var config = new ConfigurationContainer();

                config.Register<IFoo>(
                    () => new Foo(config.Resolve<IBar>()));

                config.Register<IBar>(
                    () => new Bar(config.Resolve<IFoo>()));

                config.Resolve<IFoo>();

                Assert.Fail("Configuration Exception \"Circular reference\" is expected");
            }
            catch(Exception ex)
            {
                if (!(ex is ConfigurationContainer.ConfigurationException)
                    || ex.Message.IndexOf("Circular reference", StringComparison.InvariantCultureIgnoreCase) < 0)
                    Assert.Fail("Configuration Exception \"Circular reference\" is expected instead of " + ex.GetType().Name + " '" + ex.Message + "'");
            }

        }

    }

    interface IFoo {}
    interface IBar {}

    class Foo: IFoo
    {
        private IBar _bar;

        public Foo(IBar bar)
        {
            _bar = bar;
        }
    }

    class Bar : IBar
    {
        private IFoo _foo;

        public Bar(IFoo foo)
        {
            _foo = foo;
        }
    }
}
